//== 流程管理-列表页
let processList = function () {
    let mDropZone, path;
    /**
     * 初始化列表
     */
    let initTable = function () {
        let options = {
            // 列配置
            columns: [
                {
                    field: 'id',
                    title: '#',
                    sortable: false, // 禁用此列排序
                    width: 40,
                    selector: {class: 'e-checkbox--solid'}
                },
                {
                    field: 'processDefinitionId',
                    title: 'ID',
                    sortField: 'arp.id_',
                    sortable: 'desc'
                },
                {
                    field: 'name',
                    title: '名称',
                    sortField: 'arp.name_'
                },
                {
                    field: 'key',
                    title: '标识',
                    sortField: 'arp.key_'
                },
                {
                    field: 'version',
                    title: '版本号',
                    sortField: 'arp.version_',
                    template: function (row, index, datatable) {
                        return '<span class="badge badge-brand badge--inline badge--pill">v' + row.version + '</span>';
                    }
                },
                {
                    field: 'suspensionState',
                    title: '状态',
                    dictType: 'suspensionState',
                    sortField: 'arp.suspension_state_'

                },
                {
                    field: 'Actions',
                    width: 170,
                    title: '操作',
                    sortable: false,
                    overflow: 'visible',
                    locked: {
                        right: 'md'
                    },
                    template: function (row, index, datatable) {
                        let _btn = '';
                        if (ETool.hasPermissions('activiti:process:delete')) {
                            _btn += '<a href="#" onclick="ETool.deleteById(this, \'' + row.deploymentId + '\')" class="' + ETool.ACTIONS_DANGER + '" title="删除">\
                                <i class="la la-trash"></i>\
                            </a>';
                        }
                        if (ETool.hasPermissions('activiti:process:suspend')) {
                            _btn += '<a href="#" onclick="processList.suspend(\'' + row.processDefinitionId + '\')" class="' + ETool.ACTIONS_DANGER + '" title="挂起">\
                                <i class="la la-ban"></i>\
                            </a>';
                        }
                        if (ETool.hasPermissions('activiti:process:activation')) {
                            _btn += '<a href="#" onclick="processList.activation(\'' + row.processDefinitionId + '\')" class="' + ETool.ACTIONS_SUCCESS + '" title="激活">\
                                <i class="la la-check"></i>\
                            </a>';
                        }
                        _btn += '<a target="_blank" href="' + ETool.getBaseUrl() + 'get/'+ row.deploymentId +'/' + row.dgrmResourceName + '/image/stream' + '" class="' + ETool.ACTIONS_INFO + '" title="查看流程图">\
                                <i class="la la-image"></i>\
                            </a>';
                        if (ETool.hasPermissions('activiti:model:save')) {
                            _btn += '<a href="#" onclick="processList.convertToModel(\'' + row.processDefinitionId + '\')" class="' + ETool.ACTIONS_SUCCESS + '" title="转为模型">\
                                <i class="la la-refresh"></i>\
                            </a>';
                        }
                        return _btn;
                    }
                }
            ]
        };
        processList.dataTable = ETool.initDataTable(options);
    };

    /**
     * 流程转模型
     *
     * @param processDefinitionId {string} 流程id
     */
    let convertToModel = function(processDefinitionId){
        EUtil.ajax({
            url: ETool.getBaseUrl() + 'convert/' + processDefinitionId + '/to/model',
            success: function (res) {
                ETool.successTip(commonTips.success, '模型转换成功');
            }
        });
    };

    /**
     * 绑定上传模型模态框事件
     */
    let bindUploadModal = function () {
        $('.show-upload-modal').click(function () {
            $('#upload-modal').modal();
        });
        $('#upload-modal').on('hidden.bs.modal', function (e) {
            mDropZone.removeAllFiles();
            $('#submit-model').attr('disabled', 'true');
            path = null;
        });
    };
    /**
     * 初始化文件上传
     */
    let initDropzone = function () {
        mDropZone = EApp.initDropzone({
            selector: '#upload-model',
            acceptedFiles: ".bar, .zip, .xml",
            success: function (res) {
                $('#submit-model').removeAttr('disabled');
                path = res.data.path;
            }
        })
    };

    /**
     * 提交模型
     */
    let submitModel = function () {
        if (EUtil.isNotBlank(path)) {
            EUtil.ajax({
                url: ETool.getBaseUrl() + 'add',
                data: {
                    path: path
                },
                success: function (res) {
                    // 刷新表格数据
                    processList.dataTable.reload();
                    ETool.successTip('部署成功', '上传的模型文件已部署到流程');
                }
            });
        } else {
            ETool.warnTip('部署失败', '未上传模型文件或文件未上传完毕');
        }
    };

    return {
        //== 初始化页面
        init: function () {
            ETool.setBaseUrl(basePath + '/auth/activiti/process/');
            initTable();
            initDropzone();
            bindUploadModal();
            $('#submit-model').click(function () {
                submitModel();
            });
        },
        /**
         * 流程转模型
         *
         * @param processDefinitionId {string} 流程id
         */
        convertToModel: function(processDefinitionId){
            convertToModel(processDefinitionId);
        },
        /**
         * 挂起流程
         *
         * @param processDefinitionId {string} 流程实例ID
         */
        suspend: function(processDefinitionId){
            EUtil.alertConfirm('挂起流程', '挂起流程将同时挂起流程相关的任务', function () {
                EUtil.ajax({
                    url: ETool.getBaseUrl() + 'suspend/' + processDefinitionId + '/false',
                    success: function (res) {
                        ETool.successTip(commonTips.success, '流程已挂起')
                        processList.dataTable.reload();
                    }
                });
            })
        },

        /**
         * 激活流程
         *
         * @param processDefinitionId {string} 流程实例ID
         */
        activation: function(processDefinitionId){
            EUtil.alertConfirm('激活流程', '激活流程将同时激活流程相关的任务', function () {
                EUtil.ajax({
                    url: ETool.getBaseUrl() + 'activation/' + processDefinitionId + '/false',
                    success: function (res) {
                        ETool.successTip(commonTips.success, '流程已激活')
                        processList.dataTable.reload();
                    }
                });
            })
        }
    };
}();
/**
 * 当前tab激活时是否需要重新加载数据
 *
 * @return {boolean} true/false
 */
ETab.needSubmitForm = function () {
    return true;
};
//== 初始化
$(document).ready(function () {
    processList.init();
});